package lyxs_916.rxjavatest;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ImageView;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 类说明:测试分支
 * Created by 阳  2017/3/2  23:16
 */

public class MainActivity extends AppCompatActivity {
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        imageView= (ImageView) findViewById(R.id.imview);

//        rxText1();
//        rxText2();
//        rxText3();
        rxText4();


    }

    private void rxText4() {
        Observable.create(new Observable.OnSubscribe<Drawable>() {
            @Override
            public void call(Subscriber<? super Drawable> subscriber) {
                Log.e("yy", "call--" );
                Drawable drawable=getResources().getDrawable(R.mipmap.y);
                subscriber.onNext(drawable);
                subscriber.onCompleted();
            }
        })
                .subscribeOn(Schedulers.io())// 指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<Drawable>() {
            @Override
            public void onNext( Drawable s) {
                Log.e("yy", "onNext--" +  s);

                imageView.setImageDrawable(s);
            }

            @Override
            public void onCompleted() {
                Log.e("yy", "onCompleted--" );
            }

            @Override
            public void onError(Throwable e) {
                Log.e("yy", "onError--" );

            }
        });
    }

    private void rxText3() {
        String[] name = new String[]{"77", "88", "99"};
        Observable.from(name).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.e("yy", "onNextAction--" + "call--" + s);

            }
        });
    }
    /**
     * 不完整定义的回调，RxJava 会自动根据定义创建出 Subscriber 。形式如下：
     */
    private void rxText2() {
        Action1<String> onNextAction=new Action1<String>() {
            @Override
            public void call(String s) {
                Log.e("yy", "onNextAction--"+"call--"+s);
            }
        };

        Action1<Throwable>onErrorAction=new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Log.e("yy", "onErrorAction--"+"call--");
            }
        };
        Action0 onCompletedAction=new Action0() {

            @Override
            public void call() {
                Log.e("yy", "onCompletedAction--"+"call--");
            }
        };

        Observable observable=Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("11");
                subscriber.onNext("22");
                subscriber.onNext("33");

                subscriber.onCompleted();
            }
        });

        // 自动创建 Subscriber ，并使用 onNextAction 来定义 onNext()
        observable.subscribe(onNextAction);

        // 自动创建 Subscriber ，并使用 onNextAction 和 onErrorAction 来定义 onNext() 和 onError()
        observable.subscribe(onNextAction, onErrorAction);

        // 自动创建 Subscriber ，并使用 onNextAction、 onErrorAction 和 onCompletedAction 来定义 onNext()、 onError() 和 onCompleted()
        observable.subscribe(onNextAction, onErrorAction, onCompletedAction);
    }

    
    
    
    /**
     * 两种订阅方式
     */
    private void rxText1() {
        Observable observable=Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("11");
                subscriber.onNext("22");
                subscriber.onNext("33");
                subscriber.onCompleted();
            }
        });

        final Subscriber<String> subscriber=new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Log.e("yy", "S--"+"onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.e("yy", "S--"+"onError");
            }

            @Override
            public void onNext(String s) {
                Log.e("yy","S--"+"onNext"+s);
            }
        };

        Observer<String> observer=new Observer<String>() {
            @Override
            public void onCompleted() {
                Log.e("yy", "observer--"+"onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.e("yy", "observer--"+"onError");
            }

            @Override
            public void onNext(String s) {
                Log.e("yy","observer--"+"onNext"+s);
            }
        };
        //第一种
        observable.subscribe(observer);
        //第二种
        observable.subscribe(subscriber);
    }





}
